package com.sunstar.registered.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunstar.registered.common.exception.RegisteredException;
import com.sunstar.registered.common.result.Result;
import com.sunstar.registered.common.utils.MD5;
import com.sunstar.registered.hosp.service.HospitalSetService;
import com.sunstar.registered.model.hosp.HospitalSet;
import com.sunstar.registered.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {
    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    //1.查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院信息")
    @GetMapping
    public Result<List<HospitalSet>> findAllHospSet() {
        List<HospitalSet> list = hospitalSetService.list();

        return Result.ok(list);
    }

    //2.逻辑删除医院设置
    @ApiOperation(value = "删除医院信息")
    @DeleteMapping("/{id}")
    public Result removeByIdHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //3.条件查询带分页
    @ApiOperation(value = "条件查询医院信息")
    @PostMapping("/{current}/{limit}")
    public Result<IPage<HospitalSet>> findPageHospSet(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        IPage<HospitalSet> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String hoscode = hospitalSetQueryVo.getHoscode(); //医院编号
        String hosname = hospitalSetQueryVo.getHosname(); //医院名称
        if (!StringUtils.isEmpty(hoscode)) {
            queryWrapper.eq("hoscode", hoscode);
        }
        if (!StringUtils.isEmpty(hosname)) {
            queryWrapper.like("hosname", hosname);
        }

        //调用方法实现分页查询
        IPage<HospitalSet> pageHospSet = hospitalSetService.page(page, queryWrapper);

        return Result.ok(pageHospSet);
    }

    //4.添加医院设置
    @ApiOperation(value = "添加医院信息")
    @PostMapping
    public Result saveHospSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1:使用 0:不能使用（默认为0）
        hospitalSet.setStatus(1);
        //签名秘钥
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + new Random().nextInt(1000)));

        boolean flag = hospitalSetService.save(hospitalSet);

        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5.根据id获取医院设置
    @ApiOperation(value = "获取对应医院信息")
    @GetMapping("/{id}")
    public Result<HospitalSet> getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        return Result.ok(hospitalSet);
    }

    //6.修改医院设置
    @ApiOperation(value = "修改医院信息")
    @PutMapping
    public Result<HospitalSet> updateHospSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);

        if (flag) {
            return Result.ok(hospitalSet);
        } else {
            return Result.fail(hospitalSet);
        }
    }

    //7.批量删除医院设置
    @ApiOperation(value = "批量删除医院信息")
    @DeleteMapping
    public Result batchRemoveHospSet(@RequestBody List<Long> ids) {
        boolean flag = hospitalSetService.removeByIds(ids);

        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //8.医院设置锁定和解锁
    @ApiOperation(value = "设置医院信息的锁定和解锁")
    @PutMapping("/{id}/{status}")
    public Result lockHospSet(@PathVariable Long id, @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //9.发送签名秘钥
    @ApiOperation(value = "发送签名秘钥")
    @PutMapping("/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        //模拟异常
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new RegisteredException("失败", 201);
        }
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }
}
